package com.lagou.client;

import com.lagou.model.Questions;
import com.lagou.model.Score;
import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * 编程实现客户端的主界面绘制和相应功能的实现
 */
public class ClientView {
    /**
     * 为了可以使用输入输出流，采用合成复用原则实现
     */
    private ClientInitClose cic;

    /**
     * 通过构造方法实现成员变量的初始化
     *
     * @param cic
     */
    public ClientView(ClientInitClose cic) {
        this.cic = cic;
    }

    /**
     * 自定义成员方法实现客户端主界面的绘制
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t   在线考试系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 学员系统");
            System.out.println("     [2] 管理员系统");
            System.out.println("   [0] 退出系统");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    clientUserLogin();
                    break;
                case 2:
                    clientManagerLogin();
                    break;
                case 0:
                    cic.clientClose();
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 自定义成员方法实现客户端管理员和学员登录的功能
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        // 1.准备管理员登录的相关数据
        System.out.println("请输入管理员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("managerCheck", new User(userName, password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送管理员账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            System.out.println("---------------------------------------------------------------------");
            managerMainview();
        } else {
            System.out.println("用户名或密码错误！");
        }

    }

    private void clientUserLogin() throws IOException, ClassNotFoundException {
        // 1.准备学员登录的相关数据
        System.out.println("请输入您的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入您的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("userCheck", new User(userName, password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送用户账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            System.out.println("---------------------------------------------------------------------");
            userView();
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    /**
     * 自定义成员方法实现客户端管理员功能模块：学员管理模块和考题管理模块
     */

    public void managerMainview() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t欢迎进入管理员模块");
            System.out.println("-----------------------------------------------------");
            System.out.print("   [1] 学员管理模块");
            System.out.print("     [2] 考题管理模块");
            System.out.println("     [0] 退出登陆");
            System.out.println("-----------------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            cic.getOos().write(choose);
            switch (choose) {
                case 1:
                    userManagermentMouleView();
                    break;
                case 2:
                    examQuestionsManagermentMouleView();
                    break;
                case 0:
                    System.out.println("正在退出登陆...");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    public void userManagermentMouleView() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("-------------------------------------------------------------");
            System.out.print("   [1] 添加学员信息");
            System.out.print("   [2] 删除学员信息");
            System.out.print("   [3] 修改学员信息");
            System.out.print("   [4] 查询学员信息");
            System.out.print("   [0] 返回上一层" + "\n");
            System.out.println("-------------------------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            cic.getOos().write(choose);
            cic.getOos().flush();
            switch (choose) {
                case 1:
                    addUserInfo();
                    System.out.println("添加学员信息...");
                    break;
                case 2:
                    deleteUserInfo();
                    System.out.println("删除学员信息...");
                    break;
                case 3:
                    updateUserInfo();
                    System.out.println("修改学员信息...");
                    break;
                case 4:
                    queryUserInfo();
                    System.out.println("查询学员信息...");
                    break;
                case 0:
                    System.out.println("返回上一层");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    public void examQuestionsManagermentMouleView() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("-------------------------------------------------------------");
            System.out.print("   [1] 增加考题");
            System.out.print("   [2] 删除考题");
            System.out.print("   [3] 修改考题");
            System.out.print("   [4] 查询考题");
            System.out.print("   [5] 导入考题");
            System.out.print("   [0] 返回上一层" + "\n");
            System.out.println("-------------------------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            cic.getOos().write(choose);
            cic.getOos().flush();
            switch (choose) {
                case 1:
                    addExamQuestions();
                    System.out.println("增加考题...");
                    break;
                case 2:
                    deleteExamQuestions();
                    System.out.println("删除考题...");
                    break;
                case 3:
                    updateExamQuestions();
                    System.out.println("修改考题...");
                    break;
                case 4:
                    queryExamQuestions();
                    System.out.println("查询考题...");
                    break;
                case 5:
                    importExamQuestions();
                    System.out.println("导入考题...");
                    break;
                case 0:
                    System.out.println("返回上一层");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 自定义成员方法实现学生信息的增删改查
     */

    public void addUserInfo() throws IOException {
        System.out.println("请输入新增加用户的名字:");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入新增加用户的密码:");
        String password = ClientScanner.getScanner().next();
        User user = new User(userName, password);
        cic.getOos().writeObject(user);
        cic.getOos().flush();
    }

    public void deleteUserInfo() throws IOException {
        System.out.println("请输入待删除用户的编号:");
        int index = ClientScanner.getScanner().nextInt() - 1;
        cic.getOos().write(index);
        cic.getOos().flush();
    }

    public void updateUserInfo() throws IOException {
        System.out.println("请输入修改用户的编号:");
        int index = ClientScanner.getScanner().nextInt() - 1;
        cic.getOos().write(index);
        cic.getOos().flush();
        cic.getOis().readUTF();

        //修改用户名和密码
        System.out.println("请输入修改后用户的名字:");
        String userName1 = ClientScanner.getScanner().next();
        System.out.println("请输入待修改后用户的密码:");
        String password1 = ClientScanner.getScanner().next();
        User user1 = new User(userName1, password1);
        cic.getOos().writeObject(user1);
        cic.getOos().flush();
    }

    public void queryUserInfo() throws IOException, ClassNotFoundException {
        int cnt = cic.getOis().read();
        System.out.println("系统共有用户" + cnt + "个");
        while (cnt > 0) {
            User user = (User) cic.getOis().readObject();
            System.out.println(user.toString());
            cnt--;
        }

    }

    /**
     * 自定义成员方法实现考题信息的增删改查及导入
     */

    public void addExamQuestions() throws IOException {
        System.out.println("请输入问题:");
        String question = ClientScanner.getScanner().next();
        System.out.println("请输入问题分值:");
        String score = ClientScanner.getScanner().next();
        System.out.println("请输入A选项:");
        String optionA = ClientScanner.getScanner().next();
        System.out.println("请输入B选项:");
        String optionB = ClientScanner.getScanner().next();
        System.out.println("请输入C选项:");
        String optionC = ClientScanner.getScanner().next();
        System.out.println("请输入正确答案:");
        String answer = ClientScanner.getScanner().next();
        Questions questions = new Questions(question, score, optionA, optionB, optionC, answer);
        cic.getOos().writeObject(questions);
        cic.getOos().flush();

    }

    public void deleteExamQuestions() throws IOException {
        System.out.println("请输入待删除的考题编号");
        int index = ClientScanner.getScanner().nextInt() - 1;
        cic.getOos().write(index);
        cic.getOos().flush();

    }

    public void updateExamQuestions() throws IOException {
        System.out.println("请输入待修改考题的编号:");
        int index = ClientScanner.getScanner().nextInt() - 1;
        System.out.println("请输入修改后的问题:");
        String question = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的分值:");
        String score = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的A选项:");
        String optionA = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的B选项:");
        String optionB = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的C选项:");
        String optionC = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的正确答案:");
        String answer = ClientScanner.getScanner().next();
        Questions questions = new Questions(question, score, optionA, optionB, optionC, answer);
        cic.getOos().write(index);
        cic.getOos().flush();
        cic.getOos().writeObject(questions);
        cic.getOos().flush();
    }

    public void queryExamQuestions() throws IOException, ClassNotFoundException {
        int cnt = cic.getOis().read();
        System.out.println("题库中共有" + cnt + "道题！");
        int i = 1;
        while (cnt > 0) {
            Questions questions = (Questions) cic.getOis().readObject();
            System.out.println("考题" + i + "、" + questions.toString());
            i++;
            cnt--;
        }
    }

    //获取题库
    public ArrayList<Questions> getQuestions(String str) throws IOException, ClassNotFoundException {
        File fileSource = new File("/Users/wanggege/Desktop/Database/" + str + ".txt");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileSource));
        ArrayList<Questions> questionsList = (ArrayList<Questions>) ois.readObject();
        return questionsList;
    }

    public void importExamQuestions() throws IOException, ClassNotFoundException {
        System.out.println("请选择要上传的考题");
        String str = ClientScanner.getScanner().next();
        ArrayList<Questions> questions = getQuestions(str);
        cic.getOos().writeObject(questions);
        cic.getOos().flush();
    }

    /**
     * 自定义成员方法实现客户端学员功能模块：用户模块和考试模块
     */

    public void userView() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 考试模块");
            System.out.print("   [2] 修改密码");
            System.out.print("   [0] 退出" + "\n");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            cic.getOos().write(choose);
            cic.getOos().flush();
            switch (choose) {
                case 1:
                    examModuliView();
                    System.out.println("考试模块");
                    break;
                case 2:
                    setPassword();
                    System.out.println("修改密码");
                    break;
                case 0:
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    public void examModuliView() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 开始考试");
            System.out.print("   [2] 查询成绩");
            System.out.print("   [3] 导出成绩");
            System.out.print("   [0] 返回上一层" + "\n");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            cic.getOos().write(choose);
            cic.getOos().flush();
            switch (choose) {
                case 1:
                    startExam();
                    break;
                case 2:
                    queryScoresInfo();
                    break;
                case 3:
                    exportScoreInfo();
                    System.out.println("导出成绩");
                    break;
                case 0:
                    System.out.println("返回上一层");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 自定义成员方法实现学生信息密码修改
     */
    public void setPassword() throws IOException {
        System.out.println("请输入新用户名");
        String username1 = ClientScanner.getScanner().next();
        System.out.println("请输入新密码");
        String password1 = ClientScanner.getScanner().next();
        User user1 = new User(username1, password1);
        cic.getOos().writeObject(user1);
        cic.getOos().flush();
    }

    /**
     * 自定义成员方法实现考试和成绩查询
     */

    public void startExam() throws IOException, ClassNotFoundException {
        int cnt = cic.getOis().read();
        System.out.println("本次考试共有" + cnt + "道题！");
        int i = 1;
        while (cnt > 0) {
            Questions questions = (Questions) cic.getOis().readObject();
            System.out.println("考题" + i + "、" + questions.toString());
            System.out.println("请输入你的答案:");
            String answer = ClientScanner.getScanner().next();
            cic.getOos().writeUTF(answer);
            cic.getOos().flush();
            i++;
            cnt--;
        }
        System.out.println("考试结束，祝您生活健康！");
    }

    public void queryScoresInfo() throws IOException {
        int score = cic.getOis().read();
        System.out.println("您的考试成绩为:" + score + "分");
    }

    public void exportScoreInfo() throws IOException, ClassNotFoundException {
        Score score = (Score) cic.getOis().readObject();
        File file = new File("/Users/wanggege/Desktop/Database/" + System.currentTimeMillis() + ".txt");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(score);


    }

}
